<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * QQ: 505413
 * Date: 2018/4/1
 * Time: 14:18
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Config;
use think\Db;
class Data  extends Base
{
    public function index(){
        $Db = Db::connect();
        $tmp = $Db->query('SHOW TABLE STATUS');
        $data = array_map('array_change_key_case', $tmp);
        $value['data'] = $data;
        return $this->view->assign($value ?: null)->fetch();
    }
    //备份数据库
    public function backup()
    {
        if (request()->isAjax()){
            $id = $_POST['id'];
            if (isset($id) && !empty($id)){
                $table_arr = implode(',', $id);   //备份数据表
                $config = Config::get();
                $sql = new \com\Baksql($config['database']);
                $res = $sql->backup($table_arr);
                if($res){
                    return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '备份成功']);
                }else{
                    return json(['code' => -1, 'url' => 'Data/index', 'msg' => '备份失败']);
                }
            }
        }
    }
    //备份文件列表
    public function reduction()
    {
        $config = Config::get();
        $sql = new \com\Baksql($config['database']);
        $dataList = $sql->get_filelist();
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }
    //优化数据库
    public function optimize()
    {
        if (request()->isAjax())
        {
            $id = $_POST['id'];
            $Db = Db::connect();
            if (isset($id) && !empty($id)){
                $table_arr = implode('`,`', $id);

                $res = $Db->query("OPTIMIZE TABLE `{$table_arr}`");
                if($res){
                    return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '数据表优化成功']);
                }else{
                    return json(['code' => -1, 'url' => 'Data/index', 'msg' => '数据表优化失败']);
                }

            }
        }
    }
    //修复数据库
    public function repair()
    {
        if (request()->isAjax())
        {
            $id = $_POST['id'];
            $Db = Db::connect();
            if (isset($id) && !empty($id)){
                $table_arr = implode('`,`', $id);
                $res = $Db->query("REPAIR TABLE `{$table_arr}`");
                if($res){
                    return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '数据表修复成功']);
                }else{
                    return json(['code' => -1, 'url' => 'Data/index', 'msg' => '数据表修复失败']);
                }

            }
        }
    }
    //还原数据库
    public function restore()
    {
        if (request()->isAjax()){
            $name = input('id');
            $sql = new \com\Baksql(\think\Config::get("database"));
            $res = $sql->restore($name);

            if($res){
                return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '还原成功']);
            }else{
                return json(['code' => -1, 'url' => 'Data/index', 'msg' => '还原失败，请重试']);
            }
        }
    }
    //备份下载
    public function download()
    {
        $table = input('table');
        $config = Config::get();
        $sql = new \com\Baksql($config['database']);
        $sql->downloadFile($table);
    }

    //删除备份文件
    public function delete()
    {
        $name = input('id');
        $config = Config::get();
        $sql = new \com\Baksql($config['database']);
        $res = $sql->delfilename($name);
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除失败，请重试']);
        }
    }

}